﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace APP.HELPER.Utils.CustomizeToolbox
{
    internal class resfinder
    {
    }
    
    [Description("Round (Elliptical) Button Control"), ToolboxBitmap(typeof(resfinder), "APP.HELPER.Utils.CustomizeToolbox.Properties.Resources.pictureBox0.bmp")]
    public class RoundButton : Button
    {
        private int recessDepth;
        private int bevelHeight;
        private int bevelDepth;
        private bool dome;
        private Color buttonColor;
        private string buttonText;
        private LinearGradientBrush edgeBrush;
        private Blend edgeBlend;
        private Color edgeColor1;
        private Color edgeColor2;
        private int edgeWidth;
        private int buttonPressOffset;
        private float lightAngle = 50f;
        private Color cColor = Color.White;
        private bool gotFocus = false;
        private Font labelFont;
        private VerticalString vs;
        private SolidBrush labelBrush;
        private StringFormat labelStrFmt;
        private GraphicsPath bpath;
        private GraphicsPath gpath;
        public override string Text
        {
            get
            {
                return this.buttonText;
            }
            set
            {
                this.buttonText = value;
                base.Invalidate();
            }
        }
        [Bindable(true), Category("Button Appearance"), DefaultValue(2), Description("Specifies the depth of the button's recess."), Editor(typeof(RecessEditor), typeof(UITypeEditor))]
        public int RecessDepth
        {
            get
            {
                return this.recessDepth;
            }
            set
            {
                if (value < 0)
                {
                    this.recessDepth = 0;
                }
                else
                {
                    if (value > 15)
                    {
                        this.recessDepth = 15;
                    }
                    else
                    {
                        this.recessDepth = value;
                    }
                }
                base.Invalidate();
            }
        }
        [Bindable(true), Category("Button Appearance"), DefaultValue(0), Description("Specifies the height of the button's bevel.")]
        public int BevelHeight
        {
            get
            {
                return this.bevelHeight;
            }
            set
            {
                if (value < 0)
                {
                    this.bevelHeight = 0;
                }
                else
                {
                    this.bevelHeight = value;
                }
                base.Invalidate();
            }
        }
        [Bindable(true), Category("Button Appearance"), DefaultValue(0), Description("Specifies the depth of the button's bevel.")]
        public int BevelDepth
        {
            get
            {
                return this.bevelDepth;
            }
            set
            {
                if (value < 0)
                {
                    this.bevelDepth = 0;
                }
                else
                {
                    this.bevelDepth = value;
                }
                base.Invalidate();
            }
        }
        [Bindable(true), Category("Button Appearance"), DefaultValue(false), Description("Specifies whether the button has a domed top.")]
        public bool Dome
        {
            get
            {
                return this.dome;
            }
            set
            {
                this.dome = value;
                base.Invalidate();
            }
        }
        public RoundButton()
        {
            base.Name = "RoundButton";
            base.Size = new Size(50, 50);
            base.MouseDown += new MouseEventHandler(this.mouseDown);
            base.MouseUp += new MouseEventHandler(this.mouseUp);
            base.Enter += new EventHandler(this.weGotFocus);
            base.Leave += new EventHandler(this.weLostFocus);
            base.KeyDown += new KeyEventHandler(this.keyDown);
            base.KeyUp += new KeyEventHandler(this.keyUp);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            this.buttonColor = this.BackColor;
            this.edgeColor1 = ControlPaint.Light(this.buttonColor);
            this.edgeColor2 = ControlPaint.Dark(this.buttonColor);
            Graphics graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle clientRectangle = base.ClientRectangle;
            this.edgeWidth = this.GetEdgeWidth(clientRectangle);
            this.FillBackground(graphics, clientRectangle);
            if (this.RecessDepth > 0)
            {
                this.DrawRecess(ref graphics, ref clientRectangle);
            }
            this.DrawEdges(graphics, ref clientRectangle);
            this.ShrinkShape(ref graphics, ref clientRectangle, this.edgeWidth);
            this.DrawButton(graphics, clientRectangle);
            this.DrawText(graphics, clientRectangle);
            this.SetClickableRegion();
        }
        protected void FillBackground(Graphics g, Rectangle rect)
        {
            Rectangle rect2 = rect;
            rect2.Inflate(1, 1);
            SolidBrush solidBrush = new SolidBrush(Color.FromKnownColor(KnownColor.Control));
            solidBrush.Color = base.Parent.BackColor;
            g.FillRectangle(solidBrush, rect2);
            solidBrush.Dispose();
        }
        protected virtual void DrawRecess(ref Graphics g, ref Rectangle recessRect)
        {
            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(recessRect, ControlPaint.Dark(base.Parent.BackColor), ControlPaint.LightLight(base.Parent.BackColor), this.GetLightAngle(50f));
            linearGradientBrush.Blend = new Blend
            {
                Positions = new float[]
				{
					0f,
					0.2f,
					0.4f,
					0.6f,
					0.8f,
					1f
				},
                Factors = new float[]
				{
					0.2f,
					0.2f,
					0.4f,
					0.4f,
					1f,
					1f
				}
            };
            Rectangle rect = recessRect;
            this.ShrinkShape(ref g, ref rect, 1);
            this.FillShape(g, linearGradientBrush, rect);
            this.ShrinkShape(ref g, ref recessRect, this.recessDepth);
        }
        protected virtual void DrawEdges(Graphics g, ref Rectangle edgeRect)
        {
            this.ShrinkShape(ref g, ref edgeRect, 1);
            Rectangle rect = edgeRect;
            rect.Inflate(1, 1);
            this.edgeBrush = new LinearGradientBrush(rect, this.edgeColor1, this.edgeColor2, this.GetLightAngle(this.lightAngle));
            this.edgeBlend = new Blend();
            this.edgeBlend.Positions = new float[]
			{
				0f,
				0.2f,
				0.4f,
				0.6f,
				0.8f,
				1f
			};
            this.edgeBlend.Factors = new float[]
			{
				0f,
				0f,
				0.2f,
				0.4f,
				1f,
				1f
			};
            this.edgeBrush.Blend = this.edgeBlend;
            this.FillShape(g, this.edgeBrush, edgeRect);
        }
        protected virtual void DrawButton(Graphics g, Rectangle buttonRect)
        {
            this.BuildGraphicsPath(buttonRect);
            PathGradientBrush pathGradientBrush = new PathGradientBrush(this.bpath);
            pathGradientBrush.SurroundColors = new Color[]
			{
				this.buttonColor
			};
            buttonRect.Offset(this.buttonPressOffset, this.buttonPressOffset);
            if (this.bevelHeight > 0)
            {
                Rectangle rectangle = buttonRect;
                rectangle.Inflate(1, 1);
                pathGradientBrush.CenterPoint = new PointF((float)(buttonRect.X + buttonRect.Width / 8 + this.buttonPressOffset), (float)(buttonRect.Y + buttonRect.Height / 8 + this.buttonPressOffset));
                pathGradientBrush.CenterColor = this.cColor;
                this.FillShape(g, pathGradientBrush, buttonRect);
                this.ShrinkShape(ref g, ref buttonRect, this.bevelHeight);
            }
            if (this.bevelDepth > 0)
            {
                this.DrawInnerBevel(g, buttonRect, this.bevelDepth, this.buttonColor);
                this.ShrinkShape(ref g, ref buttonRect, this.bevelDepth);
            }
            pathGradientBrush.CenterColor = this.buttonColor;
            if (this.dome)
            {
                pathGradientBrush.CenterColor = this.cColor;
                pathGradientBrush.CenterPoint = new PointF((float)(buttonRect.X + buttonRect.Width / 8 + this.buttonPressOffset), (float)(buttonRect.Y + buttonRect.Height / 8 + this.buttonPressOffset));
            }
            this.FillShape(g, pathGradientBrush, buttonRect);
            if (this.gotFocus)
            {
                this.DrawFocus(g, buttonRect);
            }
        }
        protected void DrawText(Graphics g, Rectangle textRect)
        {
            this.labelStrFmt = new StringFormat();
            this.labelBrush = new SolidBrush(this.ForeColor);
            this.labelFont = this.Font;
            this.vs = new VerticalString();
            this.vs.TextSpread = 0.75;
            bool flag = false;
            if (textRect.Height > textRect.Width * 2)
            {
                flag = true;
            }
            this.labelStrFmt.Alignment = this.ConvertToHorAlign(this.TextAlign);
            this.labelStrFmt.LineAlignment = this.ConvertToVertAlign(this.TextAlign);
            if ((!flag & this.labelStrFmt.LineAlignment != StringAlignment.Center) | (flag & this.labelStrFmt.Alignment != StringAlignment.Center))
            {
                textRect.Inflate(-(int)((double)textRect.Width / 7.5), -(int)((double)textRect.Height / 7.5));
            }
            textRect.Offset(this.buttonPressOffset, this.buttonPressOffset);
            if (!base.Enabled)
            {
                textRect.Offset(1, 1);
                this.labelBrush.Color = ControlPaint.LightLight(this.buttonColor);
                this.WriteString(flag, g, textRect);
                textRect.Offset(-1, -1);
                this.labelBrush.Color = Color.Gray;
            }
            this.WriteString(flag, g, textRect);
        }
        protected virtual void BuildGraphicsPath(Rectangle buttonRect)
        {
            this.bpath = new GraphicsPath();
            Rectangle rect = new Rectangle(buttonRect.X - 1, buttonRect.Y - 1, buttonRect.Width + 2, buttonRect.Height + 2);
            this.AddShape(this.bpath, rect);
            this.AddShape(this.bpath, buttonRect);
        }
        protected virtual void SetClickableRegion()
        {
            this.gpath = new GraphicsPath();
            this.gpath.AddEllipse(base.ClientRectangle);
            base.Region = new Region(this.gpath);
        }
        protected virtual void FillShape(Graphics g, object brush, Rectangle rect)
        {
            if (brush.GetType().ToString() == "System.Drawing.Drawing2D.LinearGradientBrush")
            {
                g.FillEllipse((LinearGradientBrush)brush, rect);
            }
            else
            {
                if (brush.GetType().ToString() == "System.Drawing.Drawing2D.PathGradientBrush")
                {
                    g.FillEllipse((PathGradientBrush)brush, rect);
                }
            }
        }
        protected virtual void AddShape(GraphicsPath gpath, Rectangle rect)
        {
            gpath.AddEllipse(rect);
        }
        protected virtual void DrawShape(Graphics g, Pen pen, Rectangle rect)
        {
            g.DrawEllipse(pen, rect);
        }
        protected virtual void ShrinkShape(ref Graphics g, ref Rectangle rect, int amount)
        {
            rect.Inflate(-amount, -amount);
        }
        protected virtual void DrawFocus(Graphics g, Rectangle rect)
        {
            rect.Inflate(-2, -2);
            this.DrawShape(g, new Pen(Color.Black)
            {
                DashStyle = DashStyle.Dot
            }, rect);
        }
        protected virtual void DrawInnerBevel(Graphics g, Rectangle rect, int depth, Color buttonColor)
        {
            Color color = ControlPaint.LightLight(buttonColor);
            Color color2 = ControlPaint.Dark(buttonColor);
            Blend blend = new Blend();
            blend.Positions = new float[]
			{
				0f,
				0.2f,
				0.4f,
				0.6f,
				0.8f,
				1f
			};
            blend.Factors = new float[]
			{
				0.2f,
				0.4f,
				0.6f,
				0.6f,
				1f,
				1f
			};
            Rectangle rect2 = rect;
            rect2.Inflate(1, 1);
            this.FillShape(g, new LinearGradientBrush(rect2, color2, color, this.GetLightAngle(50f))
            {
                Blend = blend
            }, rect);
        }
        protected float GetLightAngle(float angle)
        {
            float num = 1f - (float)base.Width / (float)base.Height;
            return angle - 15f * num;
        }
        protected int GetEdgeWidth(Rectangle rect)
        {
            int result;
            if (rect.Width < 50 | rect.Height < 50)
            {
                result = 1;
            }
            else
            {
                result = 2;
            }
            return result;
        }
        protected void WriteString(bool vertical, Graphics g, Rectangle textRect)
        {
            if (vertical)
            {
                this.vs.Draw(g, this.buttonText, this.labelFont, this.labelBrush, textRect, this.labelStrFmt);
            }
            else
            {
                g.DrawString(this.buttonText, this.labelFont, this.labelBrush, textRect, this.labelStrFmt);
            }
        }
        protected StringAlignment ConvertToHorAlign(ContentAlignment ca)
        {
            StringAlignment result;
            if (ca == ContentAlignment.TopLeft | ca == ContentAlignment.MiddleLeft | ca == ContentAlignment.BottomLeft)
            {
                result = StringAlignment.Near;
            }
            else
            {
                if (ca == ContentAlignment.TopRight | ca == ContentAlignment.MiddleRight | ca == ContentAlignment.BottomRight)
                {
                    result = StringAlignment.Far;
                }
                else
                {
                    result = StringAlignment.Center;
                }
            }
            return result;
        }
        protected StringAlignment ConvertToVertAlign(ContentAlignment ca)
        {
            StringAlignment result;
            if (ca == ContentAlignment.TopLeft | ca == ContentAlignment.TopCenter | ca == ContentAlignment.TopRight)
            {
                result = StringAlignment.Near;
            }
            else
            {
                if (ca == ContentAlignment.BottomLeft | ca == ContentAlignment.BottomCenter | ca == ContentAlignment.BottomRight)
                {
                    result = StringAlignment.Far;
                }
                else
                {
                    result = StringAlignment.Center;
                }
            }
            return result;
        }
        protected void mouseDown(object sender, MouseEventArgs e)
        {
            this.buttonDown();
        }
        protected void mouseUp(object sender, MouseEventArgs e)
        {
            this.buttonUp();
        }
        protected void buttonDown()
        {
            this.lightAngle = 230f;
            this.buttonPressOffset = 1;
            base.Invalidate();
        }
        protected void buttonUp()
        {
            this.lightAngle = 50f;
            this.buttonPressOffset = 0;
            base.Invalidate();
        }
        protected void keyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Space")
            {
                this.buttonDown();
            }
        }
        protected void keyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Space")
            {
                this.buttonUp();
            }
        }
        protected void weGotFocus(object sender, EventArgs e)
        {
            this.gotFocus = true;
            base.Invalidate();
        }
        protected void weLostFocus(object sender, EventArgs e)
        {
            this.gotFocus = false;
            this.buttonUp();
            base.Invalidate();
        }
    }
}
